package edu.task.controller;

import edu.task.common.base.BaseController;
import edu.task.common.result.RestResult;
import edu.task.model.SysProject;
import edu.task.model.SysRole;
import edu.task.model.vo.SysProjectVO;
import edu.task.model.vo.SysRoleVO;
import edu.task.repository.SysUserProjectRepository;
import edu.task.service.SysProjectService;
import edu.task.service.SysTaskService;
import edu.task.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目表控制层
 */
@RestController
@RequestMapping("/system/project")
public class SysProjectController extends BaseController {

    @Autowired
    SysProjectService projectService;

    @Autowired
    SysTaskService taskService;

    /**
     * 显示项目表所有数据
     */
    @GetMapping("/list")
    public RestResult list() {
        RestResult ajax = RestResult.success();
        ajax.put(RestResult.DATA_TAG, projectService.selectProjectList());
        return ajax;
    }

    /**
     * 项目新增
     */
    @PostMapping
    public RestResult add(@RequestBody SysProjectVO project) {
        boolean flag = projectService.insertProject(project);
        return toAjax(flag ? 1 : 0);
    }

    /**
     * 删除项目
     */
    @DeleteMapping("/{projectId}")
    public RestResult remove(@PathVariable Long projectId){
        return toAjax(projectService.deleteProjectById(projectId));
    }

    /**
     * 修改新增
     */
    @PutMapping
    public RestResult edit(@Validated @RequestBody SysProjectVO project) {
        boolean flag = projectService.updateProject(project);
        return toAjax(flag ? 1 : 0);
    }

    /**
     * 根据项目编号获取项目列表信息
     */
    @GetMapping(value = {"/","/{projectId}"})
    public RestResult getInfo(@PathVariable(value = "projectId",required = false) Long projectId){
        RestResult ajax = RestResult.success();
        if (StringUtils.isNotNull(projectId)){
            ajax.put(RestResult.DATA_TAG, projectService.selectProjectById(projectId));
            ajax.put("taskIds", taskService.selectTaskListByProjectId(projectId));
        }
        return ajax;
    }

}
